/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.utils;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * Utility class to log errors or debug information either in the plugin'log
 * or in the console. <p>
 * A set of channels is used to separate different traces for different aspects. A
 * short list is displayed at the end of the file to give an example of channels
 *<UL>
 * <LI>Editor=1 
 * <LI>ModelRessourceManager=2
 * <LI>DiagramEditPart=3
 * <LI>DiagramEditor=4
 * <LI>GraphNodeSetConstraint=5
 * <LI>PackageFigure,CollaborationFigure=6
 * <LI>GraphNodeEditPart and its subclasses =7
 * <LI>TreeView=8
 * <LI>ModelRessourceManager=9; detailed
 * <LI>Link creation=10
 * <LI>PackageAnchor=11
 * <LI>GraphEdgeEditPart=12
 * <LI>LayoutEditPolicy= 13
 * <LI>FeatureListEditPart=14
 * <LI>Factory=15
 * <LI>FeatureListFlowLayoutEditPolicy=16
 * <LI>GraphNodeCreateCommand=17
 * <LI>addCommand=17
 * <LI>EObjectPropertySource=18
 * <LI>ChooseDialog=19
 * <LI>treeEditPArt=20
 * <LI>command=21
 * <LI>ContainerWithSemanticPolicy=22
 * <LI>Property sections=23
 * <LI>VectorCalculus=24
 * <LI>DIResourceSert=25
 * <LI>LookForElement=26
 * <LI>DiagramEditPart=27
 * <LI> all figures=28
 * <LI> all Palettes=29
 * <LI>UMLElementDestroyListener=64000
 * <LI>Template=30
 * </UL>
 **/
public class Debug {
	
	/** plugin management channel */
	public static final int PLUGIN_MANAGEMENT = 50;
	
	/** Boolean set to true when all messages should be displayed */
	public static boolean debug = false;
	
	/** List of channels to display */
	private static int[] list={};

	/**
	 * Display a debug trace.
	 * @param element that provides the trace
	 * @param text message to display
	 * @param channel channel of the trace
	 */
	public static void debug (Object element,String text, int channel){
		if ((element!=null) &&(containsChannel(channel)|| debug)){
			System.out.println("("+channel+"): "+element.getClass()+ " --> "+ text);
		} else if ((element==null)&&(containsChannel(channel)|| debug)){
			System.out.println("("+channel+") --> "+ text);
		}
	}
	
	/**
	 * display error.
	 * 
	 * @param element that provides the trace
	 * @param text to display
	 * @param channel of the trace
	 */
	public static void error(Object element,String text, int channel){
		if ((element!=null) &&(containsChannel(channel)|| debug)){
			log(IStatus.ERROR, "("+channel+"): "+element.getClass()+ " --> "+ text);
		} else if ((element==null)&&(containsChannel(channel)|| debug)){
			log(IStatus.ERROR, "("+channel+") --> "+ text);
		}
	}
	
	/**
	 * Return <code>true</code> if the channel has to be displayed in the console or in the plugin's log.
	 * @param channel the current channel to test
	 * @return <code>true</code> if the message should be displayed
	 */
	public static boolean containsChannel(int channel){
		int  index =0;
		while(index<list.length){
			if (channel==list[index]){
				return true;}
			index++;
		}
		return false;
	}
	
	/**
	 * Log an exception into the plugin log.
	 * @param exception the exception to log.
	 * @param e the message to log
	 */
	public static void log(Exception e) {
		final IStatus status = new Status(IStatus.ERROR, PapyrusPlugin.PLUGIN_ID, IStatus.OK, e.getMessage(), e);
		PapyrusPlugin.getDefault().getLog().log(status);
	}
	
	/**
	 * Log a message into the plugin log.
	 * @param message the message to log
	 * @param severity the severity : one of <code>OK</code>, <code>ERROR</code>,
	 * <code>INFO</code>, <code>WARNING</code>,  or <code>CANCEL</code>
	 */
	public static void log(int severity, String message) {
		final IStatus status = new Status(severity, PapyrusPlugin.PLUGIN_ID, IStatus.OK, message, null);
		PapyrusPlugin.getDefault().getLog().log(status);
	}
}

